package cn.xej.vue.service.impl;

import cn.xej.vue.common.RespObj;
import cn.xej.vue.mapper.UserMapper;
import cn.xej.vue.pojo.User;
import cn.xej.vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceimpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public RespObj findAll() {
        Map<String,Object> map = new HashMap<>();
        List<User> userList = userMapper.findAll();
        map.put("total",10);
        map.put("totalPage",1);
        map.put("page",1);
        map.put("results",userList);
        return RespObj.build(200,"success",map);
    }

    @Override
    public RespObj delUser(String id) {
        userMapper.delUser(Integer.parseInt(id));
        return RespObj.build(200,"删除成功",null);
    }

    @Override
    public RespObj addUser(User user) {
        Map<String,Object> map = new HashMap<>();
        try {
            userMapper.addUser(user);
            map.put("success",true);
            map.put("msg","添加成功");
            return RespObj.build(200,"success",map);
        }catch (Exception e){
            map.put("success",false);
            map.put("msg","添加失败");
            return RespObj.build(400,"fail",map);
        }
    }



    @Override
    public RespObj updateUser(User user) {
        Map<String,Object> map = new HashMap<>();
        try {
            userMapper.updateUser(user);
            map.put("success",false);
            map.put("msg","添加失败");
            return RespObj.build(200,"success",map);
        }catch (Exception e){
            map.put("success",false);
            map.put("msg","添加失败");
            return RespObj.build(400,"fail",map);
        }
    }

    @Override
    public RespObj findUserById(String id) {
        User u = userMapper.findUserById(Integer.parseInt(id));

        return RespObj.build(200,"success",u);
    }
}
